package components.custom
{
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.DataGrid;
	import mx.controls.Text;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.ClassFactory;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.events.ItemClickEvent;
	import mx.utils.ObjectUtil;
	
	import spark.components.BorderContainer;
	import spark.layouts.HorizontalLayout;
	import spark.layouts.VerticalLayout;
	
	public class MenuSublist extends BorderContainer
	{
		public function MenuSublist()
		{
			super();
			layout = hLayout;
			addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
			addEventListener(FlexEvent.UPDATE_COMPLETE, updateCompleteHandler);
			addEventListener(MenuSublistItemRenderer.MENU_ITEM_CLICK, mouseDownHandler);
			setStyle("dropShadowVisible", true);
			setStyle("borderWeight", 0);
		}
		
		/**
		 * 水平布局
		 * @return 
		 * 
		 */		
		private function get hLayout():HorizontalLayout
		{
			var _layout:HorizontalLayout = new HorizontalLayout();
			_layout.paddingLeft = 6;
			_layout.paddingRight = 6;
			_layout.paddingBottom = 6;
			_layout.paddingTop = 0;
			return _layout;
		}
		private function get vLayout():VerticalLayout
		{
			var _layout:VerticalLayout = new VerticalLayout();
			return _layout;
		}
		
		/**
		 * 释放资源
		 * 
		 */		
		public function dispose():void
		{
			removeEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			removeEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
			removeEventListener(FlexEvent.UPDATE_COMPLETE, updateCompleteHandler);
			removeEventListener(MouseEvent.CLICK, mouseDownHandler);
		}
		
		/**
		 * 修正位置后回调函数
		 */		
		public var fixPositionCallback:Function;
		/**
		 * 根据菜单宽度固定位置
		 * @param p_evt
		 * 
		 */		
		private function updateCompleteHandler(p_evt:FlexEvent):void
		{
			if(stage){
				if(width >= stage.width){
					x = 0;
				}else{
					//3px为阴影部分的宽度，要补上
					if(x + width > stage.width){
						x = stage.width - width - 3;
					}					
				}
			}
			if(fixPositionCallback != null){
				fixPositionCallback(this);
				fixPositionCallback = null;
			}
		}
		
		/**
		 * 关联的对象
		 */		
		public var relativeObj:IEventDispatcher;
		
		/**
		 * 是否获取了焦点
		 */		
		public var isFocusIn:Boolean;
		
		/**
		 * 是否正在显示
		 * */
		public var isShow:Boolean;
		
		/**
		 * 数据源
		 */		
		private var _dataProvider:Object;
		public function set dataProvider(p_value:Object):void
		{
			_dataProvider = p_value;
			removeAllElements();
			createChildItems();
		}
		public function get dataProvider():Object
		{
			return _dataProvider;
		}
		
		/**
		 * 鼠标滑入事件
		 * @param p_evt
		 * 
		 */		
		private function rollOverHandler(p_evt:MouseEvent):void
		{
			isFocusIn = true;
		}
		
		/**
		 * 鼠标滑出事件
		 * @param p_evt
		 * 
		 */		
		private function rollOutHandler(p_evt:MouseEvent):void
		{
			isFocusIn = false;
			if(relativeObj)
				relativeObj.dispatchEvent(new MouseEvent(MouseEvent.ROLL_OUT));
		}
		
		private function drawLine(bx:Number, by:Number, x:Number, y:Number):UIComponent
		{
			var t:UIComponent= new UIComponent();
			var line:Sprite = new Sprite();
			line.graphics.lineStyle(1, 0xd3d3d3);
			line.graphics.moveTo(bx, by);
			line.graphics.lineTo(x, y);
			t.addChild(line); 
			return t;  
		}
		
		private function createChildItems():void
		{
			if(!_dataProvider)return;
			var tmpXml:XML = new XML(_dataProvider);
			var tmpXL:XMLList = tmpXml.elements("node");
			var cavmenu:BorderContainer = new  BorderContainer();  
			cavmenu.x = 0;
			cavmenu.y = 0;
			var hll:HorizontalLayout = new HorizontalLayout();
			hll.paddingLeft = 0;
			hll.paddingRight = 0;
			hll.gap = 0;
			cavmenu.layout = hll;
			var childXL:XMLList;
			var resultleng:int = 0;
			for(var i:int = 0;i < tmpXL.length();i++){ 
				childXL = XML(tmpXL[i]).elements("node"); 
				if(childXL.length() > resultleng){
					resultleng = childXL.length(); 
				}
			} 
			var hei:Number = (resultleng + 1)*20 + resultleng*4;
			if(tmpXL.length() > 0){
				for(i = 0;i < tmpXL.length();i++){
					var vList:DataGrid = new DataGrid();
					var ly:VerticalLayout = new VerticalLayout(); 
					vList.styleName = "topMenuSubList"; 
					vList.setStyle("horizontalGridLines", false);
					vList.headerHeight = 0;
					vList.rowHeight = 25; 
					var dc:DataGridColumn = new DataGridColumn(); 
					vList.columns = [dc]; 
					dc.itemRenderer = new ClassFactory(MenuSublistItemRenderer);  
					vList.height = hei;
					var dp:ArrayCollection = new ArrayCollection();
					var vGroup:BorderContainer = new BorderContainer();
					vGroup.setStyle("backgroundAlpha", 0);
					vGroup.setStyle("borderVisible", false); 
					var vh:VerticalLayout = new VerticalLayout(); 
					vh.gap = 0;
					vGroup.layout = vh;
					vGroup.addElement(createSubTitle(tmpXL[i].@label)); 
					childXL = XML(tmpXL[i]).elements("node");
					var len:Number = 130;
					if(childXL.length() > 0){
						for(var j:int = 0;j < childXL.length();j++){
							if(childXL[j].@visible == 'N')continue;
							dp.addItem(childXL[j]); 
							var lon:Number = (String((childXL[j] as XML).@label).length)*15;
							if(lon > len){
								len = lon;
							}
						}
					} 
					var borhei:Number = 40;
					vGroup.graphics.lineStyle(1, 0xC6D9E8); 
					vGroup.graphics.moveTo(len, 0);
					vGroup.graphics.lineTo(len, borhei); 
					vList.width = len;
					vList.dataProvider = dp;
					vGroup.addElement(vList);
					cavmenu.addElement(vGroup);   
				}  
				cavmenu.setStyle("borderVisible", false);
				addElement(cavmenu);
				styleName = "menuSubListBG"; 
			}
		}
		
		/**
		 * 垂直分割线
		 * @return 
		 * 
		 */		
		private function get verticalLine():BorderContainer
		{
			var bc:BorderContainer = new BorderContainer();
			bc.layout = vLayout;
			bc.width = 1;
			bc.percentHeight = 100;
			bc.setStyle("borderVisible", false);
			//上部分
			var bcTop:BorderContainer = new BorderContainer();
			bcTop.width = 1;
			bcTop.height = 10;
			bcTop.setStyle("borderVisible", false);
			bc.addElement(bcTop);
			//中部分
			var bcMain:BorderContainer = new BorderContainer();
			bcMain.width = 1;
			bcMain.percentHeight = 100;
			bcMain.setStyle("borderVisible", false);
			bc.addElement(bcMain);
			//上部分
			var bcBottom:BorderContainer = new BorderContainer();
			bcBottom.width = 1;
			bcBottom.height = 10;
			bcBottom.setStyle("borderVisible", false);
			bc.addElement(bcBottom);
			return bc;
		}
		
		/**
		 * 创建副标题
		 * @param p_title
		 * @return 
		 * 
		 */		
		private function createSubTitle(p_title:String):Text
		{
			var lbl:Text = new Text();
			lbl.selectable = false;
			lbl.text = p_title;
			lbl.height = 40;
			lbl.setStyle("fontSize", 14);
			lbl.setStyle("fontWeight", "bold");
			lbl.setStyle("color", "0x888888");
			lbl.setStyle("verticalAlign", "middle");
			lbl.setStyle("paddingTop", 10);
			lbl.setStyle("paddingLeft", 20);
			return lbl;
		}
		
		/**
		 * 鼠标点击链接按钮
		 * @param p_evt
		 * 
		 */		
		private function mouseDownHandler(p_evt:Event):void
		{
			p_evt.stopPropagation();
			var btn:MenuSublistItemRenderer = p_evt.target as MenuSublistItemRenderer;
			if(!btn)return;
			var evt:ItemClickEvent = new ItemClickEvent("itemClick", true);
			evt.item = ObjectUtil.copy(btn.data);
			evt.relatedObject = relativeObj as InteractiveObject;
			relativeObj.dispatchEvent(evt);
		}
	}
}